package com.jiantong.work.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.terminus.common.utils.JsonMapper;
import lombok.Data;
import org.assertj.core.util.Strings;

import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.Map;

/**
 * @author 毛文杰
 * -- 防疫中心-免疫检测
 */
@Data
public class ImmunizationDetection implements Serializable {

    private static final ObjectMapper objectMapper = JsonMapper.nonEmptyMapper().getMapper();

    private static final long serialVersionUID = 2917048338998977499L;
    /**
     *
     */
    private Long id;
    /**
     单据编号     *
     */
    private String number;
    /**
     *'状态，1：正常，-1：删除'
     */
    private Integer status;
    /**
     *养殖场
     */
    private Integer breedFactoryId;
    /**
     *养殖场名称
     */
    private String breedFactoryName;
    /**
     *'养殖区域 10：繁殖区
     */
    private Integer area;

    /**
     * 栏舍号id
     */
    private Long stableId;

    /**
     *栏舍号
     */
    private String stableNumber;
    /**
     * 抽样地点
     */
    private String place;
    /**
     * 操作人
     */
    private String operator;
    /**
     *采样日期
     */
    private Date date;
    /**
     *采样数量
     */
    private Integer quantity;
    /**
     *养殖品种：牛、马、猪
     */
    private Integer productType;
    /**
     * 样本类别：1、血样 2、尿样 3、兽药 4、饲料
     */
    private Integer sampleType;
    /**
     *单位
     */
    private Integer unit;
    /**
     *
     */
    private String otherType;
    /**
     *检测项目(疾病类型)',
     */
    private Integer type;
    /**
     *检测单位
     */
    private String detectionOrganization;
    /**
     *检测结果(合格率)',
     */
    private String qualifiedRate;
    /**
     * 合格数、可疑数、阳性数
     */
    @JsonIgnore
    private String resultJson;
    /**
     * 合格数、可疑数、阳性数Map
     */
    private Map<String , Object> resultMap;
    /**
     * 备注
     */
    private String remark;
    /**
     * '1：正常，-1：删除',
     */
    private Integer deleteFlag;
    /**
     *
     */
    private Date createdAt;
    /**
     *
     */
    private Date updatedAt;


    public void setResultJson(String resultJson){
        this.resultJson = resultJson;

        if(Strings.isNullOrEmpty(resultJson)){
            this.resultMap = null;
        }else {
            try {
                this.resultMap = objectMapper.readValue(resultJson, new TypeReference<Map<String,Object>>() {});
            } catch (IOException e) {
                //this is fuck exception
            }
        }
    }

    public void setResultMap(Map<String , Object> resultMap){
        this.resultMap = resultMap;

        if(resultMap == null || resultMap.isEmpty()){
            this.resultJson = null;
        }else {
            try {
                this.resultJson = objectMapper.writeValueAsString(resultMap);
            } catch (JsonProcessingException e) {
                //this is fuck exception
            }
        }
    }

}
